package com.pig4cloud.pig.task.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.task.entity.TaskEntity;
import com.pig4cloud.pig.task.mapper.TaskMapper;
import com.pig4cloud.pig.task.service.TaskService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 任务信息表
 *
 * @author pig
 * @date 2025-07-09 16:50:11
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, TaskEntity> implements TaskService {

    @Resource
    private TaskMapper taskMapper;
    @Override
    public  List<Map>  getFileDataByTaskId(int id) {
        // 1. 获取任务信息
        TaskEntity task = taskMapper.selectById(id);

        // 2. 获取文件路径
        String filePath = task.getExecPpk();

        // 3. 读取文件内容并直接返回
		return readJsonFile(filePath);
    }
	private List<Map> readJsonFile(String filePath){

		File file = new File(filePath);
		try {
			String content = FileCopyUtils.copyToString(new FileReader(file));
			JSONObject jsonObject = JSON.parseObject(content);

			// 直接返回data数组
			return jsonObject.getJSONArray("data").toJavaList(Map.class);
		} catch (IOException e) {
			return null;
		}
	}
}
